package cn.ffcs.uec.staff.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.uec.staff.action.bean.UecTalentTeamEditBean;
import cn.ffcs.uec.staff.model.UecTalentTeam;
import cn.ffcs.uec.staff.model.UecStaffInfo;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.NodeVo;

@Controller
@Scope("prototype")
public class UecTalentTeamEditComposer extends BasePortletComposer {

	private static final long serialVersionUID = 3086340593438520480L;
	/**
	 * 页面bean
	 */
	private UecTalentTeamEditBean bean = new UecTalentTeamEditBean();
	/**
	 * 操作类型
	 */
	private String opType;
	/**
	 * 人员信息
	 */
	private UecStaffInfo uecStaffInfo;
	/**
	 * 添加教育经历
	 */
	private UecTalentTeam uecTalentTeam;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
	}

	/**
	 * 初始化窗口
	 * @throws ParseException 
	 */
	public void onCreate$uecTalentTeamEditWindow() throws ParseException {
		this.bindCombobox();
		this.bindBean();
	}

	/**
	 * 绑定combobox
	 */
	private void bindCombobox() {
		//人才类型
		List<NodeVo> nodeVoGlbdef10List = new ArrayList<NodeVo>();
		String[][] glbdef10 = {{"1", "实操"},
				{"2", "非实操"},
				{"", "无"}};
		for (int i = 0; i < glbdef10.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(glbdef10[i][j]);
				nodeVo.setName(glbdef10[i][j + 1]);
			}
			nodeVoGlbdef10List.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getGlbdef10(), nodeVoGlbdef10List);
		
		
		// 专业人才名称glbdef1
		List<NodeVo> nodeVoGlbdef1List = new ArrayList<NodeVo>();
		String[][] glbdef1 = {{"1001A1100000001Z6UFT", "IP"},
				{"1001A1100000001Z6UFU", "市场营销"},
				{"1001A1100000001Z6UFV", "行业应用与企业信息化"},
				{"1001A1100000001Z6UFW", "无线与移动"},
				{"1001A1100000001Z6UFX", "核心网"},
				{"1001A1100000001Z6UFY", "光传输与接入"},
				{"1001A1100000001Z6UFZ", "平台与终端"},
				{"1001A1100000001Z6UG0", "网络安全"}};
		for (int i = 0; i < glbdef1.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(glbdef1[i][j]);
				nodeVo.setName(glbdef1[i][j + 1]);
			}
			nodeVoGlbdef1List.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getGlbdef1(), nodeVoGlbdef1List);

		// 专业人才等级glbdef2
		List<NodeVo> nodeVoGlbdef2List = new ArrayList<NodeVo>();
		String[][] glbdef2 = {{"1001A1100000000006OR", "A级"},
				{"1001A1100000000006OS", "B级"}, {"1001A1100000000006OT", "C级"},
				{"1001A1100000000006OU", "D级"},
				{"1001A1100000000006OV", "集团级专业人才"},
				{"1001A1100000000006OW", "IP人才"},
				{"1001A1100000000006OX", "核心网技术"},
				{"1001A1100000000006OY", "承载网技术"},
				{"1001A1100000000006OZ", "网络安全人才"},
				{"1001A1100000000006P0", "IP人才*"},
				{"1001A1100000000006P1", "无线与移动人才"},
				{"1001A1100000000006P2", "核心网人才"},
				{"1001A1100000000006P3", "光传输与接入人才"},
				{"1001A1100000000006P4", "平台与终端人才"},
				{"1001A1100000000006P5", "行业应用和企业信息化人才"},
				{"1001A1100000000006P6", "需求分析与方案设计"},
				{"1001A1100000000006P7", "系统设计与开发"},
				{"1001A1100000000006P8", "系统运营与维护"},
				{"1001A1100000000006P9", "数据挖掘与应用"},
				{"1001A1100000000006PA", "IT人才*"}};
		for (int i = 0; i < glbdef2.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(glbdef2[i][j]);
				nodeVo.setName(glbdef2[i][j + 1]);
			}
			nodeVoGlbdef2List.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getGlbdef2(), nodeVoGlbdef2List);

		// 子专业：申报专业-电信 glbdef9
		List<NodeVo> nodeVoGlbdef9List = new ArrayList<NodeVo>();
		String[][] glbdef9 = {{"1001A1100000007639R3", "市场研究"},
				{"1001A1100000007639R4", "营销策划"}, 
				{"1001A1100000007639R5", "渠道管理"},
				{"1001A1100000007639R6", "客户服务"},
				{"1001A1100000007639R7", "系统规划与架构管控-行业应用"},
				{"1001A1100000007639R8", "需求分析与方案设计-行业应用"},
				{"1001A1100000007639R9", "系统设计与开发-行业应用"},
				{"1001A1100000007639RA", "系统运营与维护-行业应用"},
				{"1001A1100000007639RB", "数据挖掘与应用-行业应用"},
				{"1001A1100000007639RC", "系统规划与架构管控-企业信息化"},
				{"1001A1100000007639RD", "需求分析与方案设计-企业信息化"},
				{"1001A1100000007639RE", "系统设计与开发-企业信息化"},
				{"1001A1100000007639RF", "系统运营与维护-企业信息化"},
				{"1001A1100000007639RG", "数据挖掘与应用-企业信息化"},
				{"1001A1100000007639RH", "平台"},
				{"1001A1100000007639RI", "核心网"},
				{"1001A1100000007639RJ", "平台与核心网*"},
				{"1001A1100000007639RK", "终端"},
				{"1001A1100000007639RL", "信息安全"}};
		for (int i = 0; i < glbdef9.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(glbdef9[i][j]);
				nodeVo.setName(glbdef9[i][j + 1]);
			}
			nodeVoGlbdef9List.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getGlbdef9(), nodeVoGlbdef9List);
		
		// 年度考核结果 glbdef3
		List<NodeVo> nodeVoGlbdef3List = new ArrayList<NodeVo>();
		String[][] glbdef3 = {{"1001A11000000000061J", "优秀"},
				{"1001A11000000000061K", "称职"}, 
				{"1001A11000000000061M", "基本称职"},
				{"1001A11000000000061M", "不称职"},
				{"1001A11000000000061M", "反应层评估"},
				{"1001A11000000000061M", "学习层评估"},
				{"1001A11000000000061M", "行为层评估"},
				{"1001A11000000000061M", "结果层评估"}};
		for (int i = 0; i < glbdef3.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(glbdef3[i][j]);
				nodeVo.setName(glbdef3[i][j + 1]);
			}
			nodeVoGlbdef3List.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getGlbdef3(), nodeVoGlbdef3List);
		
		// 聘任状态 glbdef13
		List<NodeVo> nodeVoGlbdef13List = new ArrayList<NodeVo>();
		String[][] glbdef13 = {{"1001A1100000001SVL1S", "在聘"},
				{"1001A1100000001SVL1T", "退出"}};
		for (int i = 0; i < glbdef13.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(glbdef13[i][j]);
				nodeVo.setName(glbdef13[i][j + 1]);
			}
			nodeVoGlbdef13List.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getGlbdef13(), nodeVoGlbdef13List);

	}

	/**
	 * bindBean
	 * @throws ParseException 
	 */
	public void bindBean() throws ParseException {
		opType = (String) arg.get("opType");
		if ("add".equals(opType)) {
			this.bean.getUecTalentTeamEditWindow().setTitle("新增专业人才队伍信息");
			bean.getUecTalentTeamEditDiv().setHeight("30%");
			uecStaffInfo = (UecStaffInfo) arg.get("uecStaffInfo");
		}
		if ("view".equals(opType)) {
			this.bean.getUecTalentTeamEditWindow().setTitle("查看专业人才队伍信息");
			uecTalentTeam = (UecTalentTeam) arg.get("uecTalentTeam");
			if (null != uecTalentTeam) {
				PubUtil.fillBeanFromPo(uecTalentTeam, bean);
				stringTransToDate();
			}
		}
		if ("edit".equals(opType)) {
			this.bean.getUecTalentTeamEditWindow().setTitle("编辑专业人才队伍信息");
			bean.getUecTalentTeamEditDiv().setHeight("30%");
			uecTalentTeam = (UecTalentTeam) arg.get("uecTalentTeam");
			if (null != uecTalentTeam) {
				PubUtil.fillBeanFromPo(uecTalentTeam, bean);
				stringTransToDate();
			}
		}
	}

	/**
	 * 保存
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		String msg = this.checkUacStaffData();
		if (null != msg) {
			Messagebox.show(msg);
			return;
		}
		if ("add".equals(opType)) {
			uecTalentTeam = UecTalentTeam.newInstance();
			PubUtil.fillPoFromBean(bean, uecTalentTeam);
			dataTransString();
			uecTalentTeam.setStaffId(uecStaffInfo.getStaffId());
			uecTalentTeam.addOnly();
			Events.postEvent("onOK", this.self, uecTalentTeam);
			this.onCancel();
			Messagebox.show("新增成功！");
		}
		if ("edit".equals(opType)) {
			PubUtil.fillPoFromBean(bean, uecTalentTeam);
			dataTransString();
			uecTalentTeam.updateOnly();
			Events.postEvent("onOK", this.self, uecTalentTeam);
			this.onCancel();
			Messagebox.show("更新成功！");
		}
	}

	/**
	 * 取消
	 */
	public void onCancel() {
		// 关闭窗口
		this.bean.getUecTalentTeamEditWindow().onClose();
	}

	/**
	 * 检测填入的参数是否为空
	 */
	private String checkUacStaffData() {
		// if (StrUtil.isNullOrEmpty(bean.getEncourName().getValue())) {
		// return "填写奖励名称";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourDate().getValue())) {
		// return "填写奖励日期";
		// }
		return null;
	}
	/**
	 * 一套模型，两套标准：日期类型需要格式化
	 */
	public void dataTransString() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		if (!StrUtil.isNullOrEmpty(bean.getGlbdef14().getValue())) {
			// 退出时间
			uecTalentTeam.setGlbdef14(formatter.format(bean.getGlbdef14()
					.getValue()));
		}

	}
	
	/**
	 * 将String类型的时间转换成Date类型
	 * 
	 * @throws ParseException
	 */
	public void stringTransToDate() throws ParseException {
		// 时间类型转换：String --> Date
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if (null != uecTalentTeam.getGlbdef14()) {
			bean.getGlbdef14()
					.setValue(sdf.parse(uecTalentTeam.getGlbdef14()));
		}
	}

}
